package com.ruoyi.project.pay.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateFormatUtils {

	public static String defaultFormat = "yyyy-MM-dd HH:mm:ss";
//	final static Clock clock = new NanoClock();

	public static String getCurrentDateString() {
		return getCurrentDateString(defaultFormat);
	}

	public static String getCurrentDateString(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	public static String getDateString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String getDateString_yyyy_MM_dd_HH_mm_ss(Date date) {
		return getDateString(date, defaultFormat);
	}
	
    public static String getDateString(long timeStamp, String format) {
    	return getDateString(new Date(timeStamp), format);
    }
	
    public static String getDateString(long timeStamp) {
    	return getDateString(new Date(timeStamp), defaultFormat);
    }
	
	public static String getDateString_yyyy_MM_dd_HH_mm_ss(long timeStamp) {
		SimpleDateFormat sdf = new SimpleDateFormat(defaultFormat);
		return sdf.format(new Date(timeStamp));
	}

	public static Date parseDateString(String date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}

	public static Date parseDateString_yyyy_MM_dd_HH_mm_ss(String date) throws ParseException {
		return parseDateString(date, defaultFormat);
	}

	public static java.sql.Date convert(Date date) {
		return new java.sql.Date(date.getTime());
	}

	public static java.sql.Date getSqlDate() {
		return convert(new Date());
	}

	public static java.sql.Date parseSqlDateString(String date, String format) throws ParseException {
		return convert(parseDateString(date, format));
	}

	public static java.sql.Date parseSqlDateString_yyyy_MM_dd(String date) throws ParseException {
		if (date.length() > 10)
			date = date.substring(0, 10);
		return parseSqlDateString(date, "yyyy-MM-dd");
	}

	public static java.sql.Date parseSqlDateString_yyyy_MM_dd_HH_mm_ss(String date) throws ParseException {
		if (date.length() == 10)
			date += " 00:00:00";
		else if (date.length() > 19)
			date = date.substring(0, 19);
		return parseSqlDateString(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static java.sql.Timestamp getSqlTimestamp() {
		return new java.sql.Timestamp(new Date().getTime());
	}

	public static java.sql.Timestamp parseSqlTimestampString(String date, String format) throws ParseException {
		return new java.sql.Timestamp(parseDateString(date, format).getTime());
	}

	public static java.sql.Timestamp parseSqlTimestampString_yyyy_MM_dd(String date) throws ParseException {
		if (date.length() > 10)
			date = date.substring(0, 10);
		return parseSqlTimestampString(date, "yyyy-MM-dd");
	}

	public static java.sql.Timestamp parseSqlTimestampString_yyyy_MM_dd_HH_mm_ss(String date) throws ParseException {
		if (date.length() == 10)
			date += " 00:00:00";
		else if (date.length() > 19)
			date = date.substring(0, 19);
		return parseSqlTimestampString(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String getCronExpression(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String cron = second + " " + minute + " " + hour + " " + day + " " + month + " ? " + year;
        return cron;
	}
	
	public static String addHours(String date, int count) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDateString(date, defaultFormat));
		calendar.add(Calendar.HOUR, count);
		return getDateString(calendar.getTime(), defaultFormat);
	}

	public static Date addHours(Date date, int count) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, count);
		return calendar.getTime();
	}
	
	public static String addMinutes(String date, int count) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDateString(date, defaultFormat));
		calendar.add(Calendar.MINUTE, count);
		return getDateString(calendar.getTime(), defaultFormat);
	}

	public static Date addMinutes(Date date, int count) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, count);
		return calendar.getTime();
	}
	
	public static String addDays(String date, int count) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDateString(date, defaultFormat));
		calendar.add(Calendar.DATE, count);
		return getDateString(calendar.getTime(), defaultFormat);
	}

	public static Date addDays(Date date, int count) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, count);
		return calendar.getTime();
	}
	
	public static String addMonths(String date, int count) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDateString(date, defaultFormat));
		calendar.add(Calendar.MONTH, count);
		return getDateString(calendar.getTime(), defaultFormat);
	}

	public static Date addMonths(Date date, int count) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, count);
		return calendar.getTime();
	}
	
	public static int getDayOfWeek(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseDateString(date, "yyyy-MM-dd"));
		return calendar.get(Calendar.DAY_OF_WEEK);
	}
	
//    public static long getCurrentNanos() {
//	    Instant instant = Instant.now(clock);
//	    return instant.getEpochSecond() * 1000000000 + instant.getNano();
//	}
    /**
     * 获取本月第一天
     * @return
     */
	public static String getBeginTimeOfThisMonth() {
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return getDateString(calendar.getTime(), "yyyy-MM-dd") + " 00:00:00";
	}
	
	/**
     * 获取本月最后一天
     * @return
     */
    public static String getEndTimeOfThisMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getDateString(calendar.getTime(), "yyyy-MM-dd") + " 23:59:59";
    }
    
    public static void main(String[] args) {
		System.out.println(getBeginTimeOfThisMonth());
		System.out.println(getEndTimeOfThisMonth());
	}
}
